package com.yixiang.np.controller;

import com.yixiang.np.constant.GlobalConstant;
import com.yixiang.np.model.Grid;
import com.yixiang.np.model.Json;
import com.yixiang.np.model.PageFilter;
import com.yixiang.np.model.SessionInfo;
import com.yixiang.np.model.po.PltmNotice;
import com.yixiang.np.service.NoticeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * Created by Administrator on 2016/11/1.
 */
@Controller
@RequestMapping("/notice")
public class NoticeController {

    private static final Logger LOG = LoggerFactory.getLogger(NoticeController.class);

    @Resource
    private NoticeService noticeService;

    @ResponseBody
    @RequestMapping(value = "/noticePage", method = RequestMethod.GET)
    public Grid noticePage(@ModelAttribute PltmNotice pltmNotice, @ModelAttribute PageFilter pageFilter) {
        Integer page = pageFilter.getPage();
        Integer rows = pageFilter.getRows();
        LOG.debug("pltmNotice:{}; page:{}; rows:{};", pltmNotice, page, rows);
        Grid g = noticeService.noticePage(pltmNotice, page, rows);
        LOG.debug("Grid:{};", g);
        return g;
    }

    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Json add(PltmNotice pltmNotice, HttpSession session) {
        LOG.debug("pltmNotice:{}; ", pltmNotice);
        Json j = new Json();
        try {
            SessionInfo info = (SessionInfo) session.getAttribute(GlobalConstant.SESSION_INFO);
            pltmNotice.setCreateUser(info.getId().intValue());
            pltmNotice.setCreateTime(new Date());
            pltmNotice.setLastModifyTime(new Date());
            pltmNotice.setLastModifyPerson(info.getId().intValue());
            pltmNotice.setCreateName(info.getName());
            noticeService.create(pltmNotice);
            j.setSuccess(true);
            j.setMsg("创建通知成功！");
        } catch (Exception e) {
            j.setMsg("创建通知失败！");
        }
        LOG.debug("json:{};", j);
        return j;
    }

    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Json update(PltmNotice pltmNotice, HttpSession session) {
        LOG.debug("pltmNotice:{}; ", pltmNotice);
        Json j = new Json();
        try {
        	PltmNotice notict = noticeService.read(new PltmNotice(pltmNotice.getId()));
            SessionInfo info = (SessionInfo) session.getAttribute(GlobalConstant.SESSION_INFO);
            notict.setContent(pltmNotice.getContent());
            notict.setTitle(pltmNotice.getTitle());
            notict.setLastModifyTime(new Date());
            notict.setLastModifyPerson(info.getId().intValue());
            noticeService.update(notict);
            j.setSuccess(true);
            j.setMsg("更新通知成功！");
        } catch (Exception e) {
            j.setMsg("更新通知失败！");
        }
        LOG.debug("json:{};", j);
        return j;
    }

    @ResponseBody
    @RequestMapping(value = "/del/{id}", method = RequestMethod.POST)
    public Json  del(@PathVariable Long id) {
        LOG.debug("id:{}; ", id);
        Json j = new Json();
        try {
            noticeService.delete(new PltmNotice(id));
            j.setSuccess(true);
            j.setMsg("删除通知成功！");
        } catch (Exception e) {
            j.setMsg("删除通知失败！");
        }
        LOG.debug("json:{};", j);;
        return j;
    }

    @ResponseBody
    @RequestMapping(value = "/read/{id}", method = RequestMethod.GET)
    public Json read(@PathVariable Long id) {
        LOG.debug("id:{}; ", id);
        Json j = new Json();
        try {
            PltmNotice pltmNotice = noticeService.read(new PltmNotice(id));
            j.setSuccess(true);
            j.setObj(pltmNotice);
            j.setMsg("查询通知成功！");
        } catch (Exception e) {
            j.setMsg("查询通知失败！");
        }
        LOG.debug("json:{};", j);
        return j;
    }
    @RequestMapping(value = "/noticeEdit/{id}")
    public String noticeEdit(@PathVariable Long id,HttpServletRequest request) {
    	 PltmNotice pltmNotice = noticeService.read(new PltmNotice(id));
    	 request.setAttribute("notice", pltmNotice);
        return "/admin/noticeEdit";
    }
    @RequestMapping(value = "/view/{id}")
    public String noticeView(@PathVariable Long id,HttpServletRequest request) {
    	 PltmNotice pltmNotice = noticeService.read(new PltmNotice(id));
    	 request.setAttribute("notice", pltmNotice);
        return "/admin/noticeView";
    }
    @RequestMapping(value = "/outer/{id}")
    public String outerView(@PathVariable Long id,HttpServletRequest request) {
    	 PltmNotice pltmNotice = noticeService.read(new PltmNotice(id));
    	 request.setAttribute("notice", pltmNotice);
        return "/np/noticeViewOuter";
    }
}
